import customTab from "../../../wxjs/customTab";
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  sportDataAppid: app.sportData.appid,
  sportDataPath: app.sportData.path,
  whochangeSportAppid: app.whochangeSport.appid,
  whochangeSportPath: app.whochangeSport.path,
};

var currentPage = {
  data,
  onLoad: function(options) {
    const that = this;
    console.log(app.userInfo)
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;

  },
  onShow: function() {
    const that = this;
    app.infoLog("onShow isToOrder===>", app.isToOrder);
    that.getUser();
    that.setData({
      count: app.notifyCount
    })
    if (!app.notifyCount) {
      wx.hideTabBarRedDot({
        index: 2,
      })
    }
    if (app.isToOrder) {
      setTimeout(() => {
        wx.navigateTo({
          url: app.xhx_route.orderList,
        })
      }, 500)
      app.isToOrder = false;
    }
  },
  navToNext,
  getUser,
  changeChild,//选择孩子
  navigateToMiniProgram,
};

var page = Object.assign({}, currentPage);

Page(page);

function navToNext(event) {
  const that = this;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  let params = event.currentTarget.dataset;
  const url = params.url;
  const unskip = params.unskip;
  if (unskip) return false;
  wx.navigateTo({
    url: `${app.xhx_route[url]}?${paramFormat(params)}`,
  })

}

function paramFormat(params) {
  let url = '';
  for (let key in params) {
    if (key == 'url' || key == 'unskip') continue;
    if (url) url += `&`;
    url += `${key}=${params[key] || ''}`
  }

  return url;
}


function getUser() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.babyDetailList,
    cancelToast: true,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      var temp = res.data || {};
      var babyInfo = wx.getStorageSync("babyInfo") || {};
      var childList = temp.childList || [];
      var applicationCountList = temp.applicationCountList || [];
      var fansCountList = temp.fansCountList || [];
      var guardianCountList = temp.guardianCountList || [];
      var likesCountList = temp.likesCountList || [];

      childList.forEach(item => {
        applicationCountList.forEach(childItem => {
          if (item.userId == childItem.child_user_id) {
            item.applicationCount = childItem.applicationCount || 0;
          }
        })
        fansCountList.forEach(childItem => {
          if (item.userId == childItem.child_user_id) {
            item.fansCount = childItem.fansCount || 0;
          }
        })
        guardianCountList.forEach(childItem => {
          if (item.userId == childItem.userId) {
            item.guardianCount = childItem.guardianCount || 0;
          }
        })
        likesCountList.forEach(childItem => {
          if (item.userId == childItem.userId) {
            item.likesCount = childItem.likesCount || 0;
          }
        })
      })

      if (!babyInfo.userId) {
        wx.setStorageSync("babyInfo", childList[0]);
        babyInfo = childList[0];
      }
      
      that.setData({
        childList,
        babyInfo,
      })
    },
    fail: function(res) {
      app.showToast(res && res.msg || "加载失败");
    }
  })
}

function changeChild(event){
  const that = this;
  const index = event.currentTarget.dataset.index || 0;
  const childList = that.data.childList || [];
  wx.setStorageSync("babyInfo", childList[index]);
  that.setData({
    babyInfo: childList[index],
  })
}

function navigateToMiniProgram(event) {
  const that = this;
  const appId = event.currentTarget.dataset.appid;
  const path = event.currentTarget.dataset.path;

  wx.navigateToMiniProgram({
    appId,
    path,
  })
}